﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilsORM;
using UtilsORM.MySqlData;
using UtilsCache;

namespace ServiceWeb.DBGateway
{
    public class DBIotDeviceLiveState
    {
        public bool getProductGateWayIsOnline(string pguid)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            var dguids = (from d in ef.iot_device
                          join m in ef.iot_device_model on d.d_model_guid equals m.m_guid
                          where m.m_attribute == 3 && d.d_p_guid == pguid && d.d_status == 0
                          select d).Select(c => c.d_guid);
            foreach (var guid in dguids)
            {
                iot_device_live_state live_State = getGateWayIsOnline(guid);
                if (live_State == null)
                {
                    return false;
                }
            }
            return true;

        }
        public iot_device_live_state getGateWayIsOnline(string gwdguid)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            return ef.iot_device_live_state.Where(c => c.ls_d_guid == gwdguid && c.ls_live_state_set_time > DateTime.Now.AddMinutes(-2)).FirstOrDefault();
        }


    }
}
